package hk.ust.comp3111.ustnutrition;

import hk.ust.comp3111.ustnutrition.data.DataAccessor;
import hk.ust.comp3111.ustnutrition.data.db.Food;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.caverock.androidsvg.SVGImageView;

public class HistoryItemView extends LinearLayout
{
	public HistoryItemView(Context context)
	{
		super(context);
	}

	public HistoryItemView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	@SuppressLint("NewApi")
	public HistoryItemView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	}

	@SuppressWarnings("deprecation")
	public void setTime(Date time)
	{
		ViewHolder vh = (ViewHolder)getTag();
		if (time != null)
		{
			vh.hourView.setText(String.format("%02d", time.getHours()));
			//vh.minView.setText(String.format("%02d", time.getMinutes()));
			vh.minView.setText("00");
		}
		else
		{
			vh.hourView.setText("");
			vh.minView.setText("");
		}
	}

	public void setActive(boolean isActive)
	{
		ViewGroup separatorContainer = (ViewGroup)findViewById(
				R.id.history_separator);
		if (isActive)
		{
			SVGImageView separator = new SVGImageView(getContext());
			separator.setImageResource(R.raw.history_separator_active);
			separatorContainer.removeAllViews();
			separatorContainer.addView(separator,
					(int)MiscUtils.dpToPx(getContext(), 16),
					(int)MiscUtils.dpToPx(getContext(), 80));
		}
		else
		{
			SVGImageView separator = new SVGImageView(getContext());
			separator.setImageResource(R.raw.history_separator_passive);
			separatorContainer.removeAllViews();
			separatorContainer.addView(separator,
					(int)MiscUtils.dpToPx(getContext(), 16),
					(int)MiscUtils.dpToPx(getContext(), 16));
		}
	}

	public void removeAllFoods()
	{
		if (!mTasks.isEmpty())
		{
			for (DownloadImageTask t : mTasks)
			{
				t.cancel(true);
			}
		}
		mTasks.clear();

		ViewHolder vh = (ViewHolder)getTag();
		vh.foodContainer.removeAllViews();
	}

	public void addFood(Food food)
	{
		ViewHolder vh = (ViewHolder)getTag();
		ImageView iv = new ImageView(getContext());
		int size = (int)MiscUtils.dpToPx(getContext(), 72);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				size, size);
		params.gravity = Gravity.CENTER_VERTICAL;
		params.rightMargin = (int)MiscUtils.dpToPx(getContext(), 8);
		vh.foodContainer.addView(iv, params);

		showFoodImage(food, iv);
	}

	@Override
	protected void onFinishInflate()
	{
		super.onFinishInflate();
		ViewHolder vh = new ViewHolder() {};
		vh.hourView = (TextView)findViewById(R.id.time_h);
		vh.minView = (TextView)findViewById(R.id.time_m);
		vh.foodContainer = (LinearLayout)findViewById(R.id.food_container);
		setTag(vh);
	}

	private static class ViewHolder
	{
		public TextView hourView;
		public TextView minView;
		public LinearLayout foodContainer;
	}

	private void showFoodImage(Food food, final ImageView view)
	{
		if (food == null || food.getPhoto() == null)
		{
			view.setImageResource(android.R.drawable.ic_menu_gallery);
			view.setScaleType(ScaleType.CENTER);
			return;
		}

		int size = (int)MiscUtils.dpToPx(getContext(), 72);
		DownloadImageTask t = new DownloadImageTask(size, size)
				{
					@Override
					protected void onPostExecute(Bitmap result)
					{
						if (result != null)
						{
							view.setImageBitmap(result);
							view.setScaleType(ScaleType.CENTER_CROP);
						}
						else
						{
							view.setImageResource(
									android.R.drawable.ic_menu_gallery);
							view.setScaleType(ScaleType.CENTER);
						}
					}
				};
		t.execute(DataAccessor.Instance.getResource(food.getPhoto()));
		mTasks.add(t);
	}

	private List<DownloadImageTask> mTasks = new ArrayList<DownloadImageTask>();
}
